import card as cd
from typing import List

class Game():
    def __init__(self, id):
        self.playerReady = [False, False]
        self.playerCards = [[],[]]

        self.phase1 = [False, False]
        self.Phase2 = [False, False]
        self.settlement = [False, False]

        self.cards = cd.Cards()
        self.ready = False
        self.id = id
        self.turns = 0
        self.scores = [0,0]
        self.wins = [0,0]

    def connected(self):
        return self.ready

    def bothReady(self):
        return self.playerReady[0] and self.playerReady[1]

    def deal_cards(self, num = 12):
        if num%2!=0:
            print("剩余牌数不为偶数")
        else:
            self.playerCards = [self.cards.deal_cards(num/2), self.cards.deal_cards(num/2)]

    def set_player(self, p, reday):
        self.playerReady[p] = ready

    def get_player_cards(self, p, cards):
        self.playerCards[p] = cards
        if self.turns == 4:
            for _ in range(3):
                self.playerCards[p].insert(0, None)
        self.playerReady[p] = True
        
    def compare(self, flags:List[bool]):
        #1
        if flags[0]:
            if self.playerCards[0][0] < self.playerCards[1][0]:
                self.scores[0]+=2
            else:
                self.scores[1]+=2

        #2
        if flags[1]:
            self.p1Game2 = cd.Game2(self.playerCards[0][1:3])
            self.p2Game2 = cd.Game2(self.playerCards[1][1:3])
            if self.p1Game2 < self.p2Game2 :
                self.scores[0] += 4
            else:
                self.scores[1] += 4

        #3
        if flags[2]:
            self.p1Game3 = cd.Game3(self.playerCards[0][3:6])
            self.p2Game3 = cd.Game3(self.playerCards[1][3:6])
            if self.p1Game3 < self.p2Game3 :
                self.scores[0] += 6
            else:
                self.scores[1] += 6
    
    def play_phase1(self, p):
        self.set_player(p, False)
        if not self.phase1[0] and not self.phase1[1]:
            if self.turns < 4:
                self.deal_cards()
            elif self.turns ==4:
                self.deal_cards(6)
        self.phase1[p] = True
    
    def play_phase2(self, p):
        if not self.phase2[0] and not self.phase2[1]:
            if self.turns < 4:
                self.compare([True, True, True])
            elif self.turns == 4:
                self.compare([False, False, True])
            self.turns += 1
        self.phase2[p] = True

    def turns_settlement(self, p):
        self.phase1[p], self.phase2[p] = False, False
        if not self.settlement[p]:
            if self.turns == 5:

                if self.scores[0] > self.scores[1]:
                    self.wins[0] += 1
                elif self.scores[0] < self.scores[1]:
                    self.wins[1] += 1
                else:
                    self.tie += 1

                self.playerCards = [[],[]]
                self.cards = cd.Cards()
                self.turns = 0
        self.settlement[p] = True







